let config = require('./config.js');
import functions from './functions.js';
function request(url, param = {}, way = 'POST', contentType = true) {
	let token = functions.getLocalStorage('token');
	param.sn = config.SN;
	param._ = (new Date()).getTime();
	let data = {};
	if (param) {
		for (let key in param) {
			if (param[key] == null || param[key] == 'null') {
				delete param[key]
			}
		}
		data = { ...data,...param}
	}
	let header = {};
	let sign = functions.getSign(data)
	if (contentType == true) {
		header = {
			"content-type": "application/x-www-form-urlencoded",
			"Sign":sign,
			"Access-Token": token
		}
	} else {
		header = {
			"content-type": "application/json;charset=UTF-8",
			"Sign":sign,
			"Access-Token": token
		}
	}
	console.info("config",config)
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.API_URL + url,
			data: data,
			header: header,
			method: way,
			success: (response) => {
				let result = response.data;
				if (result.code == 1) {
					resolve(result);
				} else if (result.code == -2) {
					wx.setStorageSync('authToken', '')
					functions.checkSession();
				} else {
					reject(result);
				}
			},
			fail: (error) => {
				functions.showToast('数据加载异常!');
			}
		});
	})
}
export default request
